#include "TrackLayer.h"
#include "LoadResource.h"
#include "YWConutGameLayer.h"
#include "PublicClass.h"
using namespace cocos2d;

static char nString[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";


TrackLayer::TrackLayer(std::string trackstring):trackString(trackstring),pngString(".png")
{
	this->init();
	this->initData();
	this->loadTexture();
	this->initPosition();
}


TrackLayer::~TrackLayer(void)
{
}

void TrackLayer::initData(){
	winSize = CCDirector::sharedDirector()->getWinSize();
	this->setContentSize(CCSize::CCSize(winSize.width*2,winSize.height));	
	showList = new struct ShowList;
	showList->pSprite = NULL;
	showList->next = new struct ShowList;
	showList->next->pSprite = NULL;
	showList->next->next= new struct ShowList;
	showList->next->next->pSprite = NULL;
	showList->next->next->next = NULL;

	ic = 0;
	nextStyle = 1;
	nextNum = 0 ;

	this->initNums();
	this->initIndex();

	
}

void TrackLayer::initNums(){
	sNum.anum = SINGLENUMS;
	sNum.bnum = COUPLENUMS;
	sNum.cnum = THREENUMS;

	sNum.abnum = SINGLENUMS+COUPLENUMS;
	sNum.abcnum = SINGLENUMS+COUPLENUMS+THREENUMS;

	sNum.abnums = SINGLENUMS + COUPLENUMS*2;
	sNum.abcnums = SINGLENUMS + COUPLENUMS*2 + THREENUMS*3;
}




void TrackLayer::initIndex(){
	index = new int[sNum.abcnum];
	for (int i =0 ;i<=SINGLENUMS;i++)
	{
		index[i] = i;
	}
	//int sum = SINGLENUMS + COUPLENUMS;
	for (int i = SINGLENUMS+1;i<=sNum.abnum;i++)
	{
		index[i] = index[i-1]+2;
	}
	//sum = SINGLENUMS + COUPLENUMS + THREENUMS;
	for (int i = SINGLENUMS + COUPLENUMS +1;i<sNum.abcnum ;i++)
	{
		index[i] = index[i-1] + 3;
	}
}


void TrackLayer::loadTexture(){
	LoadTextureResource *load = LoadTextureResource::getShare();
	ICC *icc = ICC::getShareIcc();
	//CCTexture2D * texture[TRACKSPRITESNUMS];
	std::string recourceString;
	//for (int i =0;i<TRACKSPRITESNUMS;i++)
	//{
	//	recourceString = trackString+nString[i]+pngString;
	//	texture[i] = CCTextureCache::sharedTextureCache()->addImage(recourceString.c_str());
	//	texture[i]->setAliasTexParameters();
	//	CCLOG(recourceString.c_str());
	//}
	//float sx = winSize.width/texture[0]->getContentSize().width;
	//float sy = winSize.height/texture[0]->getContentSize().height;

	for (int i = 0;i<TRACKSPRITESNUMS;i++)
	{
		sprites[i] = CCSprite::createWithTexture(load->trackSprites[i]);
		sprites[i]->setScaleX(icc->getwholeScaleX());
		sprites[i]->setScaleY(icc->getwholeScaleY());
		sprites[i]->setVisible(false);
		this->addChild(sprites[i]);
	}
}

void TrackLayer::initPosition(){
	int i=0,j=1;
 
	showList->pSprite = sprites[i];
	showList->next->pSprite = sprites[j];
	showList->next->next->pSprite = sprites[2];
	showList->pSprite->setVisible(true);
	showList->pSprite->setPosition(ccp(winSize.width/2,winSize.height/2));

	showList->next->pSprite->setVisible(true);
	showList->next->pSprite->setPosition(ccp(winSize.width*1.5f,winSize.height/2));

	showList->next->next->pSprite->setVisible(true);
	showList->next->next->pSprite->setPosition(ccp(winSize.width*2.5f,winSize.height/2));
}

cocos2d::CCSprite *TrackLayer::getNextSprite(){
	int j;

	if (nextStyle ==1)
	{
		j=getNextNum();
		//CCLog("the next is %d",j);
		preNum = j;
		ICC *icc = ICC::getShareIcc();
		int tempi = converToBox2DNum(j);
		//CCLog("the restroe next is %d",tempi);
		icc->setNext(tempi);
		if (j < SINGLENUMS)//
		{
			nextNum = 0;
			return sprites[j];
		} 
		else if(j >= SINGLENUMS && j <sNum.abnums)
		{
			nextNum = j+1;
			nextStyle = 2;
			return sprites[j];
		}
		else if(j >=(sNum.abnums) )
		{
			nextNum = j+1;
			nextStyle = 3;
			return sprites[j];
		}
	} 
	else if(nextStyle ==2)
	{
		nextStyle = 1;
		return sprites[nextNum];
	}
	else if (nextStyle ==3)
	{
		if (isSecend(nextNum))
		{
			return sprites[nextNum++];
		} 
		else
		{
			nextStyle = 1;
			return sprites[nextNum];
		}
	}

	return NULL;
}

bool TrackLayer::isSecend(int num){
	int k =num - 1;
	for (int i= 0 ;i<(sNum.abcnum);i++)
	{
		if (index[i]==  k)
		{
			return true;
		}
	}
	return false;
}



int TrackLayer::getNextNum(){
	int j;
	do 
	{
		j = rand()%(sNum.abcnum);
	} while (j==0 || sprites[index[j]] == showList->pSprite || sprites[index[j]] ==showList->next->pSprite || index[j]==preNum);
	//cocos2d::CCLog("the j value:%d,the pre Num is %d",j,preNum);
	return index[j];
}


void TrackLayer::updateShowSprites(struct ShowList *pShow,float n){
	pShow->pSprite->setPosition(ccp(0,0));
	pShow->pSprite->setVisible(false);

	pShow->pSprite = getNextSprite();

	pShow->pSprite->setVisible(true);
	pShow->pSprite->setPosition(ccp(winSize.width*2.5f-n,winSize.height/2));
}


void TrackLayer::draw(){
	ICC *icc = ICC::getShareIcc();
	this->backgroundMove(icc->getBackValue()*PTM_RATIO);
	
	icc->moveOver();
	ic = 0;
}

void TrackLayer::backgroundMove(float xlength)
{
	((YWConutGameLayer*)(PublicClass::GetPublic()->getPoint(LayerCount)))->m_pSpriteQieGaoCount->ChangeNum(xlength/15);
	if (showList->pSprite->getPositionX() <= (0-winSize.width/2))
	{
		float n = (0-winSize.width/2) - showList->pSprite->getPositionX();
		updateShowSprites(showList,n);
	} 
	else if (showList->next->pSprite->getPositionX() <= (0-winSize.width/2))
	{
		float n = (0-winSize.width/2) - showList->next->pSprite->getPositionX();
		updateShowSprites(showList->next,n);
	}
	else if (showList->next->next->pSprite->getPositionX() <= (0-winSize.width))
	{
		float n = (0-winSize.width/2) - showList->next->next->pSprite->getPositionX();
		updateShowSprites(showList->next->next,n);
	}
	showList->pSprite->setPositionX(showList->pSprite->getPositionX()-xlength);
	showList->next->pSprite->setPositionX(showList->next->pSprite->getPositionX()-xlength);
	showList->next->next->pSprite->setPositionX(showList->next->next->pSprite->getPositionX()-xlength);
}

int TrackLayer::converToBox2DNum( int i )
{
	if (i < SINGLENUMS)
	{
		return i;
	} 
	else if(i >= SINGLENUMS && i<sNum.abnums)
	{
		return (SINGLENUMS + (i-SINGLENUMS)/2);
	}
	else
	{
		return(SINGLENUMS+COUPLENUMS+(i-sNum.abnums)/3);
	}
}
